package com.cloud.service.impl;

import com.cloud.entity.Employee;
import com.cloud.entity.PageInfo;
import com.cloud.mapper.EmpMapper;
import com.cloud.service.EmpService;
import com.cloud.utils.GlobalConstant;
import com.cloud.utils.Md5Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author .
 * created in 2020/1/11 7:54
 */

@Service
public class EmpServiceImpl implements EmpService {

    private Logger logger = LoggerFactory.getLogger(EmpServiceImpl.class);

    private final EmpMapper empMapper;

    public EmpServiceImpl(EmpMapper empMapper) {
        this.empMapper = empMapper;
    }

    @Override
    public int addEmp(Employee employee) {
        Date curTime = new Date();
        employee.setCode(UUID.randomUUID().toString().replace("-", ""));
        employee.setPassword(Md5Tools.numberMd5("123456"));
//        employee.setCreater("管理员01");
        employee.setCtime(curTime);
        employee.setStatus(1);
        employee.setLock(1);
        employee.setLtime(curTime);
//        employee.setUpdater("管理员01");
        employee.setUtime(curTime);
        System.out.println(employee);
        int result = empMapper.addEmp(employee);
        if (result > 0) {
            logger.info("员工【{}】添加成功", employee.getName());
            return GlobalConstant.SUCCESS;
        }
        logger.info("员工【{}】添加失败", employee.getName());
        return GlobalConstant.ERROR;
    }

    @Override
    public int delEmp(String ids) {
        if (ids.contains(",")) {
            String[] idList = ids.split(",");
            List<Integer> id = new ArrayList<>();
            for (String s : idList) {
                id.add(Integer.parseInt(s));
            }
            int result = empMapper.delEmpByIds(id);
            return result == idList.length ? GlobalConstant.SUCCESS : GlobalConstant.ERROR;
        }
        int id = Integer.parseInt(ids);
        return empMapper.delEmpById(id);
    }

    @Override
    public int updateEmp(Employee employee) {
        System.out.println(employee);
        if (StringUtils.isEmpty(employee.getLock())) employee.setLock(1);
        else employee.setLock(0);
        System.out.println(employee);
//        employee.setUpdater("管理员01");
        employee.setUtime(new Date());
        int result = empMapper.updateEmp(employee);
        if (result > 0) {
            logger.info("员工【{}】信息更新成功", employee.getName());
            return GlobalConstant.SUCCESS;
        }
        logger.info("员工【{}】信息更新失败", employee.getName());
        return GlobalConstant.ERROR;
    }

    @Override
    public PageInfo<Employee> findAll(PageInfo<Employee> info, String name) {
        if ("".equals(name)) name = null;
        info.setCount(empMapper.findEmpCount(name));
        info.setData(empMapper.findAll((info.getPage() - 1) * info.getLimit(), info.getLimit(), name));
        return info;
    }

    @Override
    public Employee findEmpByCondition(Employee employee) {
        if (!StringUtils.isEmpty(employee.getPassword()))
            employee.setPassword(Md5Tools.numberMd5(employee.getPassword()));
        return empMapper.findEmpByCondition(employee);
    }

}
